package io.springboot.jpa.examples.springbootjpaexamples.web;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;

/**
 * @author yzc
 * @description 统一的HTTP JSON消息格式
 * @date 2018/9/26
 */
public class ResultMap extends HashMap implements Serializable {

    private static final long serialVersionUID = 8374860266221259679L;

    public ResultMap() {
        this.put("status", 200);
        this.put("error", null);
        this.put("message", null);
        this.put("data", null);
        this.put("timestamp", new Date());

    }

    public ResultMap ok() {
        this.put("status", 200);
        this.put("message", "success");
        return this;
    }
    public ResultMap fail() {
        this.put("status", 500);
        this.put("error", "error");
        return this;
    }
    public ResultMap fail(int status, String error) {
        this.put("status", status);
        this.put("error", error);
        return this;
    }
    public ResultMap status(int status) {
        this.put("status", status);
        return this;
    }
    public ResultMap error(String error) {
        this.put("error", error);
        return this;
    }
    public ResultMap msg(String msg) {
        this.put("message", msg);
        return this;
    }
    public ResultMap data(Object data) {
        this.put("data", data);
        return this;
    }

}
